#include "bloginfo.h"

extern QPixmap pixmapToRound(QPixmap &src, int radius);
extern QString authorHead;
extern QString authorName;

BlogInfo::BlogInfo(QWidget *parent): QWidget(parent)
{
    QWidget *widget1 = new QWidget();
    QWidget *widget2 = new QWidget();
    QWidget *widget3 = new QWidget();
    QHBoxLayout *hLayout1 = new QHBoxLayout();
    QHBoxLayout *hLayout2 = new QHBoxLayout();
    QHBoxLayout *hLayout3 = new QHBoxLayout();
    QVBoxLayout *vLayout = new QVBoxLayout();

    // 第一行， 显示头像、用户名
    label_header = new QLabel();
    label_username = new QLabel(authorName);
    QPixmap px(authorHead); // 缩放
    QPixmap dst = px.scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    dst = pixmapToRound(dst, 25);
    label_header->setPixmap(dst);
    hLayout1->addWidget(label_header);
    hLayout1->addWidget(label_username);
    label_username->setAlignment(Qt::AlignCenter);
    widget1->setLayout(hLayout1);

    // 第二行，显示粉丝、点赞、收藏、评论
    label_fans = new QLabel("11\n粉丝");
    label_comment = new QLabel("22\n评论");
    label_praise = new QLabel("33\n点赞");
    label_collection = new QLabel("44\n收藏");
    //hLayout2->addWidget(label_fans);
    hLayout2->addWidget(label_comment);
    hLayout2->addWidget(label_praise);
    hLayout2->addWidget(label_collection);
    label_fans->setAlignment(Qt::AlignCenter);
    label_comment->setAlignment(Qt::AlignCenter);
    label_praise->setAlignment(Qt::AlignCenter);
    label_collection->setAlignment(Qt::AlignCenter);
    widget2->setLayout(hLayout2);

    // 第三行，显示关注按钮
    QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Ignored);
    label_care = new QLabel("关注");
    label_care->setStyleSheet(QString::fromUtf8("border:3px solid blue; border-radius: 10px;"));
    label_care->setMinimumWidth(70);
    label_care->setAlignment(Qt::AlignCenter);
    hLayout3->addSpacerItem(spacer);
    //hLayout3->addWidget(label_care);
    hLayout3->addSpacerItem(spacer);
    widget3->setLayout(hLayout3);

    vLayout->addWidget(widget1);
    vLayout->addWidget(widget2);
    vLayout->addWidget(widget3);

    // 热门博客
    hotBlog = new HotBlog();
    //vLayout->addWidget(hotBlog);
    hotBlog->addItem("234");
    hotBlog->addItem("345");
    hotBlog->addItem("456");

    QSpacerItem *spacer1 = new QSpacerItem(20, 40, QSizePolicy::Ignored, QSizePolicy::Expanding);
    vLayout->addItem(spacer1);

    // 加载整体布局
    setLayout(vLayout);

    strList << "header" << "username" << "fans" << "comment" << "praise" << "collection" << "care";
    labelList << label_header << label_username << label_fans << label_comment << label_praise << label_collection << label_care;
    for(int i=0; i<labelList.length(); i++)
    {
        labelList.at(i)->installEventFilter(this);
    }
}

void BlogInfo::setAuthorName(QString name)
{
    label_username->setText(name);
}
void BlogInfo::setAuthorHead(QString path)
{
    QPixmap px(path); // 缩放
    QPixmap dst = px.scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    dst = pixmapToRound(dst, 25);
    label_header->setPixmap(dst);
}
void BlogInfo::setOtherInfo(QString info1, QString info2, QString info3)
{
    label_praise->setText(info1+QString("\n点赞"));
    label_collection->setText(info2+QString("\n收藏"));
    label_comment->setText(info3+QString("\n评论"));
}

// 事件过滤器，实现点击效果
bool BlogInfo::eventFilter(QObject *obj, QEvent *event)
{
    for(int i=0; i<labelList.length(); i++)
    {
        if(obj == labelList.at(i))
        {
            // 鼠标进入
            if(event->type() == QEvent::Enter)
            {
                if(obj == label_care)
                {
                    label_care->setStyleSheet(QString::fromUtf8("border:3px solid blue; border-radius: 10px; background-color:rgb(0, 250, 102);"));
                }
                else
                {
                    labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
                }
            }
            // 鼠标离开
            else if (event->type() == QEvent::Leave)
            {
                if(obj == label_care)
                {
                    label_care->setStyleSheet(QString::fromUtf8("border:3px solid blue; border-radius: 10px; background-color:rgb(237,243,247);"));
                }
                else
                {
                    labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(237,243,247);}");
                }

            }
            // 鼠标单击事件
            else if(event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                // 左键单击
                if(mouseEvent->button() == Qt::LeftButton)
                {

                    // 发射信号
                    emit labelClicked(strList.at(i));
                    return true;

                }
                else
                {
                    return false;
                }
            }
        }
    }

    return QObject::eventFilter(obj, event);
}

BlogInfo::~BlogInfo()
{

}
